﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace Athena.Codec.Implementations
{
    using Message;
    public class JsonRpcMessageDecoder : IRpcMessageDecoder
    {
        public RpcTransportMessage MessageDecoding(byte[] message)
        {
            var messageJsonString = Encoding.UTF8.GetString(message);
            return MessageDecoding(messageJsonString);
        }

        public RpcTransportMessage MessageDecoding(string message)
        {
            var rpcTransportMessage = JsonConvert.DeserializeObject<RpcTransportMessage>(message);
            if (rpcTransportMessage.IsInvokeMessage)
            {
                rpcTransportMessage.Content = JsonConvert.DeserializeObject<RpcInvokeMessage>(rpcTransportMessage.Content.ToString());
            }
            if (rpcTransportMessage.IsInvokeResultMessage)
            {
                rpcTransportMessage.Content = JsonConvert.DeserializeObject<RpcInvokeResultMessage>(rpcTransportMessage.Content.ToString());
            }
            return rpcTransportMessage;
        }
    }
}
